﻿using AutoMapper;
using MediatR;
using Traceable.App.Application.Command.Goods;
using Traceable.App.DTOS;
using Traceable.Domian;
using Traceable.ErrorCode;
using Traceable.Infastructure;

namespace Traceable.App.Application.Handler.Goods
{
    public class GetGoodsHandler : IRequestHandler<GetGoodsCommand,ApiResult<List<GoodsDto>>>
    {
        private readonly IBaseRepository<Goods_Table> _goodsReposiitory;
        private readonly IMapper _mapper;

        public GetGoodsHandler(IBaseRepository<Goods_Table> goodsReposiitory, IMapper mapper)
        {
            _goodsReposiitory = goodsReposiitory;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取商品名称
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<List<GoodsDto>>> Handle(GetGoodsCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<GoodsDto>>result = new ApiResult<List<GoodsDto>>();

            var goods =_goodsReposiitory.GetAll().Where(p=>!p.IsDelete);
            result.Code=StatusEnum.OK;
            result.Message = "获取成功";
            result.Data=_mapper.Map<List<GoodsDto>>(goods);
            return Task.FromResult(result);
        }
    }
}
